﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.AdvTree;
using DevComponents.DotNetBar;
using PPI.Client.Logic;
using PPI.Entities;
using ColumnHeader=DevComponents.AdvTree.ColumnHeader;

namespace PPI.Controls.Grid
{
    public partial class GPSGrid : GridBase
    {
        public List<EVendor_Visit_XMissionStructure> CurrentVisits
        {
            get { return m_CurrentVisits; }
        }
        private List<EVendor_Visit_XMissionStructure> m_CurrentVisits;
        private ElementStyle m_poleNodeStyle;

        public GPSGrid()
        {
            InitializeComponent();
            m_ColumnId = "ID";

            m_poleNodeStyle = new ElementStyle();
            m_poleNodeStyle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(234)))), ((int)(((byte)(239)))), ((int)(((byte)(242)))));
        }

        public void LoadVisits(string connectionString)
        {

            m_CurrentVisits = LVendor_Visit_XMissionStructure.GetMobile(m_OutboxBase);

            //ColumnHeader chVisit = new ColumnHeader("Visit");
            //chVisit.Width.Absolute = 50;

            //ColumnHeader chProject = new ColumnHeader("Project");
            //chProject.Width.Absolute = 100;

            //ColumnHeader chContact = new ColumnHeader("Contact");
            //chContact.Width.Absolute = 100;

            //ColumnHeader chType = new ColumnHeader("Type");
            //chType.Width.Absolute = 100;

            //ColumnHeader chLatitude = new ColumnHeader("Latitude");
            //chLatitude.Width.Absolute = 100;

            //ColumnHeader chLongitude = new ColumnHeader("Longitude");
            //chLongitude.Width.Absolute = 100;

            //ColumnHeader chAltitude = new ColumnHeader("Altitude");
            //chAltitude.Width.Absolute = 100;

            //ColumnHeader chTime = new ColumnHeader("Time");
            //chTime.Width.Absolute = 100;

            //ColumnHeader chSatellites = new ColumnHeader("Satellites");
            //chSatellites.Width.Absolute = 50;



            //treeGrid.Columns.Add(chVisit);
            //treeGrid.Columns.Add(chProject);
            //treeGrid.Columns.Add(chContact);
            //treeGrid.Columns.Add(chType);
            //treeGrid.Columns.Add(chLatitude);
            //treeGrid.Columns.Add(chLongitude);
            //treeGrid.Columns.Add(chAltitude);
            //treeGrid.Columns.Add(chTime);
            //treeGrid.Columns.Add(chSatellites);

            //foreach (EVendor_Visit_XMissionStructure visit in m_CurrentVisits)
            //{
            //    Node visitNode = CreateVisitNode(visit);
            //    treeGrid.Nodes.Add(visitNode);
            //}


            datagridBase.AutoGenerateColumns = true;

            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
            dt.Columns.Add(new DataColumn("Project", typeof(string)));
            dt.Columns.Add(new DataColumn("Contact", typeof(string)));
            dt.Columns.Add(new DataColumn("Type", typeof(string)));
            dt.Columns.Add(new DataColumn("Latitude", typeof(double)));
            dt.Columns.Add(new DataColumn("Longitude", typeof(double)));
            dt.Columns.Add(new DataColumn("Altitude", typeof(double)));
            dt.Columns.Add(new DataColumn("Time", typeof(DateTime)));
            dt.Columns.Add(new DataColumn("Satellites", typeof(Int32)));

            foreach (EVendor_Visit_XMissionStructure visit in m_CurrentVisits)
            {
                dt.Rows.Add(visit.Id, visit.Vendor_Project.Name, visit.Vendor_Contact.Name, visit.Vendor_Visit_Type.Name,
                            visit.Latitude, visit.Longitude, visit.Altitude, visit.SatelliteTime, visit.SatelliteCount);
            }
            datagridBase.DataSource = dt;

            datagridBase.Columns[0].HeaderText = "Visit";

            datagridBase.Refresh();



        }

        private Node CreateVisitNode(EVendor_Visit_XMissionStructure visit)
        {
            Node visitNode = new Node();

            visitNode.Text = visit.Id.ToString();
            visitNode.Cells.Add(new Cell(visit.Vendor_Project.Name));
            visitNode.Cells.Add(new Cell(visit.Vendor_Contact.Name));
            visitNode.Cells.Add(new Cell(visit.Vendor_Visit_Type.Name));
            visitNode.Cells.Add(new Cell(visit.Latitude.ToString()));
            visitNode.Cells.Add(new Cell(visit.Longitude.ToString()));
            visitNode.Cells.Add(new Cell(visit.Altitude.ToString()));
            visitNode.Cells.Add(new Cell(visit.SatelliteTime.ToShortDateString()));
            visitNode.Cells.Add(new Cell(visit.SatelliteCount.ToString()));

            ColumnHeader chPoleId = new ColumnHeader("ID");
            chPoleId.Width.Absolute = 100;

            ColumnHeader chToptype = new ColumnHeader("Top Type");
            chToptype.Width.Absolute = 50;

            ColumnHeader chSpecie = new ColumnHeader("Species");
            chSpecie.Width.Absolute = 50;

            ColumnHeader chTreatment = new ColumnHeader("Treatment");
            chTreatment.Width.Absolute = 50;

            ColumnHeader chYear = new ColumnHeader("Year");
            chYear.Width.Absolute = 50;

            ColumnHeader chClass = new ColumnHeader("Class");
            chClass.Width.Absolute = 50;

            ColumnHeader chLatitude = new ColumnHeader("Latitude");
            chLatitude.Width.Absolute = 100;

            ColumnHeader chLongitude = new ColumnHeader("Longitude");
            chLongitude.Width.Absolute = 100;

            ColumnHeader chAltitude = new ColumnHeader("Altitude");
            chAltitude.Width.Absolute = 100;

            ColumnHeader chSatellites = new ColumnHeader("Satellites");
            chSatellites.Width.Absolute = 50;

            ColumnHeader chAID = new ColumnHeader("AID");
            chAID.Width.Absolute = 50;

            ColumnHeader chLvisit = new ColumnHeader("Last visit");
            chLvisit.Width.Absolute = 50;

            ColumnHeader chNumber = new ColumnHeader("Number");
            chNumber.Width.Absolute = 50;

            ColumnHeader chLength = new ColumnHeader("Length");
            chLength.Width.Absolute = 50;

            ColumnHeader chLVisitor = new ColumnHeader("Last Visitor");
            chLVisitor.Width.Absolute = 100;


            visitNode.NodesColumns.Add(chPoleId);
            visitNode.NodesColumns.Add(chToptype);
            visitNode.NodesColumns.Add(chSpecie);
            visitNode.NodesColumns.Add(chTreatment);
            visitNode.NodesColumns.Add(chYear);
            visitNode.NodesColumns.Add(chClass);
            visitNode.NodesColumns.Add(chLatitude);
            visitNode.NodesColumns.Add(chLongitude);
            visitNode.NodesColumns.Add(chAltitude);
            visitNode.NodesColumns.Add(chSatellites);
            visitNode.NodesColumns.Add(chAID);
            visitNode.NodesColumns.Add(chLvisit);
            visitNode.NodesColumns.Add(chNumber);
            visitNode.NodesColumns.Add(chLength);
            visitNode.NodesColumns.Add(chLVisitor);


            foreach (EVendor_Visit_XMissionStructures_Pole pole in visit.Poles)
            {
                Node  poleNode = new Node();
                poleNode.Style = m_poleNodeStyle;
                poleNode.Text = pole.Id.ToString();
                poleNode.Cells.Add(new Cell(pole.TopType.Name.ToString()));
                poleNode.Cells.Add(new Cell(pole.Species.Name.ToString()));
                poleNode.Cells.Add(new Cell(pole.Treatment.Name.ToString()));
                poleNode.Cells.Add(new Cell(pole.Year.ToString()));
                poleNode.Cells.Add(new Cell(pole.Class.Name.ToString()));
                poleNode.Cells.Add(new Cell(pole.Latitude.ToString()));
                poleNode.Cells.Add(new Cell(pole.Longitude.ToString()));
                poleNode.Cells.Add(new Cell(pole.Altitude.ToString()));
                poleNode.Cells.Add(new Cell(pole.SatelliteCount.ToString()));
                poleNode.Cells.Add(new Cell(pole.AID));
                poleNode.Cells.Add(new Cell(pole.LVisit.ToString()));
                poleNode.Cells.Add(new Cell(pole.Number));
                poleNode.Cells.Add(new Cell(pole.Length.ToString()));
                poleNode.Cells.Add(new Cell(pole.LVisitor));

                visitNode.Nodes.Add(poleNode);
            }
            return visitNode;

        }
    }
}
